# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Contributor: Patrycja Rosa <alpine@ptrcnull.me>
# Maintainer: Will Sinatra <wpsinatra@gmail.com>
pkgname=woodpecker
pkgver=2.7.1
pkgrel=2
pkgdesc="Simple yet powerful CI/CD engine with great extensibility"
url="https://woodpecker-ci.org"
arch="all"
license="Apache-2.0"
pkgusers="woodpecker"
pkggroups="woodpecker"
makedepends="
	go
	go-swag
	sqlite-dev
	pnpm
	"
subpackages="
	$pkgname-agent
	$pkgname-agent-openrc:agent_openrc
	$pkgname-cli
	$pkgname-doc
	$pkgname-openrc
	"
install="$pkgname.pre-install"
source="$pkgname-$pkgver.tar.gz::https://github.com/woodpecker-ci/woodpecker/releases/download/v$pkgver/woodpecker-src.tar.gz
	woodpecker-server.initd
	woodpecker-agent.initd
	woodpecker-server.conf
	woodpecker-agent.conf
	woodpecker-server.logrotate
	woodpecker-agent.logrotate
	fix-makefile.patch
	"
options="net"

# s390x fails
case "$CARCH" in
	s390x) options="$options !check" ;;
esac

export GOFLAGS="$GOFLAGS -tags=libsqlite3"
export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

unpack() {
	mkdir -p $builddir
	tar -xzvf $srcdir/$pkgname-$pkgver.tar.gz -C $builddir
}

build() {
	export TARGETARCH="$(go env GOHOSTARCH)"
	make build -j1 STATIC_BUILD=false
	make docs
}

check() {
	make test
}

package() {
	# Install server
	install -Dm755 "$builddir"/dist/woodpecker-server \
		"$pkgdir"/usr/bin/woodpecker-server

	# Install initd & conf
	install -Dm755 "$srcdir"/woodpecker-server.initd \
		"$pkgdir"/etc/init.d/woodpecker-server
	install -Dm640 -o woodpecker -g woodpecker "$srcdir"/woodpecker-server.conf \
		"$pkgdir"/etc/woodpecker/server.conf

	# Install web resources
	install -d -m 755 -o woodpecker -g woodpecker "$pkgdir"/usr/share/webapps/$pkgname
	cp -R "$builddir"/web/dist/* "$pkgdir"/usr/share/webapps/$pkgname/

	# Create log directory
	mkdir -p "$pkgdir"/var/log/woodpecker

	# Install logrotate rules
	install -Dm644 "$srcdir"/woodpecker-server.logrotate \
		"$pkgdir"/etc/logrotate.d/woodpecker-server

	# Install mandocs
	mkdir -p "$pkgdir"/usr/share/doc/$pkgname
	cp -R "$builddir"/docs/docs/* "$pkgdir"/usr/share/doc/$pkgname/
}

cli() {
	pkgdesc="Woodpecker Server CLI"

	install -Dm755 "$builddir"/dist/woodpecker-cli \
		"$subpkgdir"/usr/bin/woodpecker-cli
}

agent() {
	pkgdesc="Woodpecker CI agent"

	install -Dm755 "$builddir"/dist/woodpecker-agent \
		"$subpkgdir"/usr/bin/woodpecker-agent

	install -Dm640 -o woodpecker -g woodpecker "$srcdir"/woodpecker-agent.conf \
		"$subpkgdir"/etc/woodpecker/agent.conf

	# Install logrotate rules
	install -Dm644 "$srcdir"/woodpecker-agent.logrotate \
		"$pkgdir"/etc/logrotate.d/woodpecker-agent

	# Create log directory
	mkdir -p "$subpkgdir"/var/log/woodpecker
}

agent_openrc() {
	pkgdesc="Woodpecker CI agent (OpenRC init scripts)"
	depends=openrc
	install_if="openrc $pkgname-agent=$pkgver-r$pkgrel"

	install -Dm755 "$srcdir"/woodpecker-agent.initd \
		"$subpkgdir"/etc/init.d/woodpecker-agent
}

sha512sums="
56673770311aae745a50221ce67d9cf4bb4f199325c30e8a24652c3f4bb8e8efba5b0e85b43195bb13cfdf3f8be6a8351ffa49bf04486792ae8f73bb5da9a965  woodpecker-2.7.1.tar.gz
02cb0d2182846df9d650b7c3282195c61a59d2cc52386073d755414183384bc24c744b70e1e2ac6e62045a8840fabeffa0fed905c9d93d5c6a7b3100644386f6  woodpecker-server.initd
77b52280e3564e9b3f2429c60b310cdf43307b0379b24cd1e53c2b0523e5483d044f484ab521ac9d25482b2bdbd28147b8bb765116011b7bae85a6aa79275fd8  woodpecker-agent.initd
b6c317731c8ae10376019ab0bba7313ddb1b6c495f214de60e58be11c1303245a6c9caf84c3c3ed6290908acc8b6c4d3305f16d72ca66e0349b0c4db7c4d8166  woodpecker-server.conf
33fc8cdb3821325cc061f32ace86aef1a3d5e43ae617058ee95ccd1955feef4923d49c716107c87daf03ee050586a1017fc106108d34e9cc6d101f25d0022b57  woodpecker-agent.conf
e6fbecc95f9e0be9f3c0410492e58167e6aa2a7c47a02446585484cb8f506a6e84ea61f35e656dd335ad9bd297a3dd842b1333bdc8f3581786b3e67029fe7a44  woodpecker-server.logrotate
e6fbecc95f9e0be9f3c0410492e58167e6aa2a7c47a02446585484cb8f506a6e84ea61f35e656dd335ad9bd297a3dd842b1333bdc8f3581786b3e67029fe7a44  woodpecker-agent.logrotate
132ed42fbaa0e227d6722fd36057b8c49ad5abc4e90245d7e80b668323fe7bb8db978e5b9751c540b112ae017c02c25b220f1dca6cd32b5952bec166b03b0938  fix-makefile.patch
"
